package com.comiee.tankwar;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
/**
 * 坦克类
 * @author comiee
 *
 */
public class Tank extends Sprite {
	private Sprite blood0 = new Sprite(new Texture("images/blood0.png"));
	private Sprite blood1 = new Sprite(new Texture("images/blood1.png"));
	private OrthographicCamera camera=null;
	private float speed=10;	//坦克的速度
	private int cd=0;	//开火间隔
	boolean live=true;	//是否存活
	int maxHealth=10;	//最大血量
	int health=10;	//血量
	int score;	//分数
	List<Missile> missiles;	//子弹
	
	public Tank(OrthographicCamera camera) {	//玩家的构造方法
		super(new Texture("images/tank_p.png"));
		super.setPosition(14*64, 3*64);
		this.missiles = new ArrayList<Missile>();
		this.camera = camera;
		camera.position.set(this.getX()+this.getOriginX(),this.getY()+this.getOriginY(),0);
	}
	
	public Tank(List<Sprite> all,int health) {	//敌人的构造方法
		super(new Texture("images/tank_e.png"));
		start:while(true) {			//标号只能用于循环语句前
			Random rand = new Random();
			super.setPosition(rand.nextInt(Gdx.graphics.getWidth()), rand.nextInt(Gdx.graphics.getHeight()));
			for(Sprite i : all) {
				if (i==this) continue;
				if (super.getBoundingRectangle().overlaps(i.getBoundingRectangle())) continue start;
			}
			break;
		}
		this.missiles = new ArrayList<Missile>();
		this.health = health;
		this.maxHealth=health;
	}
	
	public void move(float px,float py,List<Sprite> all) {
		//内置的getBoundingRectangle()方法的返回值会随着旋转改变，可能导致卡住
		if (overlap(this,all)) {
			super.translate(px*speed, py*speed);	//如果移动前已经碰撞，则仍然可以移动
		}
		else {
			super.translate(px*speed, 0);	//尝试X方向移动
			if (overlap(this,all)) {	//如果碰撞
				super.translate(-px*speed, 0);	//取消移动
			}
			super.translate(0, py*speed);	//尝试Y方向移动
			if (overlap(this,all)) {	//如果碰撞
				super.translate(0, -py*speed);	//取消移动
			}
		}
		super.setRotation((float) (Math.atan2(py,px)*180/Math.PI-90));	//设置方向
		if (camera!=null) camera.position.set(this.getX()+this.getOriginX(),this.getY()+this.getOriginY(),0);	//设置相机
	}
	private boolean overlap(Sprite x, List<Sprite> all) {
		for (Sprite i : all) {
			if (x==i) continue;
			Rectangle p = new Rectangle(x.getX()+5,x.getY()+5,x.getWidth()-5,x.getHeight()-5);
			Rectangle e = new Rectangle(i.getX(),i.getY(),i.getWidth(),i.getHeight());
			if (p.overlaps(e)) return true;
		}
		return false;
	}

	public void fire() {
		if (cd==0) {
			missiles.add(new Missile(this));
			this.cd=10;
		}
	}
	
	public void hite(Tank enemy) {
		enemy.health -= 1;
		if (enemy.health==0) {
			enemy.live=false;
			this.score++;
		}
	}
	
	@Override
	public void draw(Batch batch) {
		//显示坦克
		super.draw(batch);
		//显示子弹
		List<Missile> temp = new ArrayList<Missile>();	//存储死亡状态的子弹
		for(Missile i : missiles) {
			i.move();
			if (i.live) i.draw(batch);
			else temp.add(i);
		}
		missiles.removeAll(temp);
		if (cd>0) cd--;	//开火间隔
		//显示血条
		Sprite blood;
		for(int i=0;i<maxHealth;i++) {
			if (i<health) blood=blood1;
			else blood=blood0;
			blood.setPosition(this.getX()+this.getOriginX()-blood.getWidth()*(maxHealth/2-i), this.getY()-blood.getHeight());
			blood.setOrigin(this.getX()+this.getOriginX()-blood.getX(), this.getY()+this.getOriginY()-blood.getY());
			blood.setRotation(this.getRotation());
			blood.draw(batch);
		}
	}
	
	public void dispose() {
		super.getTexture().dispose();
		this.blood0.getTexture().dispose();
		this.blood1.getTexture().dispose();
	}
	
	Tank e;	//AI的目标
	public void AI(List<Tank> tanks, List<Sprite> all) {
		if (e==null) e=tanks.get(new Random().nextInt(tanks.size()));
		if (distance(e)>512) this.moveto(e, all);
		if(new Random().nextInt(100)<2) {
			e=tanks.get(new Random().nextInt(tanks.size()));	//随机选择一个坦克
			this.moveto(e, all);
			this.fire();
		}
		//仇恨//**//
	}
	private void face(Sprite sprite) {
		float x1 = this.getX()+this.getOriginX();
		float y1 = this.getY()+this.getOriginY();
		float x2 = sprite.getX()+sprite.getOriginX();
		float y2 = sprite.getY()+sprite.getOriginY();
		double r = Math.atan2(y2-y1,x2-x1)*180/Math.PI-90;
		this.setRotation((float) r);
	}
	private double distance(Sprite sprite) {
		return Math.sqrt(Math.pow(this.getX()-sprite.getX(),2)+Math.pow(this.getY()-sprite.getY(),2));
	}
	private void moveto(Sprite sprite, List<Sprite> all) {
		this.face(sprite);
		double r = (this.getRotation()+90)*Math.PI/180;
		this.move((float)Math.cos(r), (float)Math.sin(r), all);
	}
}
